package org.itboys.coo.service.mongo.house;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.coo.conmon.Finals;
import org.itboys.coo.entity.mongo.house.CooHouse;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2017/11/30
 * 描述：迈迪龙_户型
 */
@Service
public class CooHouseService extends BaseMongoService<CooHouse>{
    @Resource(name="mdlDs")
    private MongoDataSource mongoDataSource;
    @Autowired
    private SundayUploadService uploadService;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return mongoDataSource;
    }
    @Override
    protected Class<CooHouse> getEntityClass() {
        return CooHouse.class;
    }
    /**
     * 查询
     * @param request
     * @return
     */
    public PageResult<CooHouse> selectHouse(HttpServletRequest request){
        Map<String ,Object> param= ParamUtil.packageMongoExactParam(request);
        Map<String ,String> containparam=ParamUtil.packageMongoVagueParam(request);
        param.put("isDeleted", Finals.NO);
        return super.containpageQuery(request,containparam,param);
    }
    /**
     * 保存  或修改户型
     * @param house
     * @param imageFile
     * @throws IOException
     */
    public void saveHouse(CooHouse house, MultipartFile imageFile) throws IOException {
        //保存主图
        Map<String, String> map = uploadService.uploadSingle(imageFile);
        if(StringUtils.isNotEmpty(map.get(uploadService.UPLOAD_HALF_PATH))){
            house.setImage(map.get(uploadService.UPLOAD_HALF_PATH));
        }
        if(LongUtil.isNotZreo(house.getId())){
            super.updateExceptEmpty(house.getId(),house);
        }else {
            super.save(house);
        }
    }
    /**
     * 查询单个
     * @param houseId
     * @return
     */
    public CooHouse findOne(Long houseId){
        CooHouse house=null;
        if(LongUtil.isNotZreo(houseId)){
            house=super.getById(houseId);
        }else {
            house=new CooHouse();
        }
        return house;
    }

    /**
     * 设置为推荐
     * @param houseId
     * @param type 1，首页推荐。2其他待定
     */

    public void setRecommend(Long houseId,Integer type){
        CooHouse house=super.getById(houseId);
        house.setIsIndexRecommend(type);
        super.updateExceptEmpty(houseId,house);
    }
    /**
     * 删除户型
     * @param houseId
     */
    public void deleteHouse(Long houseId){
        super.delete(houseId);
        logger.info("删除户型","id="+houseId);
    }

}
